<template>
    <div class="foundlist">
      <div class="search_add">
        <el-form :inline="true" ref="search_data" :model='search_data' >
          <el-form-item label="时间筛选:">
            <el-date-picker
                v-model="search_data.startTime"
                type="datetime"
                placeholder="选择开始时间">
            </el-date-picker> --
            <el-date-picker
                v-model="search_data.endTime"
                type="datetime"
                placeholder="选择结束时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
              <el-button type="primary" size ="small" icon="search" @click='onScreeoutMoney()'>筛选</el-button>
          </el-form-item>
          <el-form-item class="rightBtn" v-if="user.identity == 'manager'">
            <el-button type="primary" @click="handelAdd()" size="small" icon="view">添加</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="table_container">
        <el-table
          :data="tableData"
          max-height="450"
          border
          :default-sort = "{prop: 'date', order: 'descending'}"
          v-if="tableData.length > 0"
          style="width: 100%">
          <el-table-column
            :show-overflow-tooltip="true"
            type="index"
            label="序号"
            align='center'
            fixed
            width="70">
          </el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            fixed
            prop="date"
            sortable
            label="创建时间"
            align='center'
            width="250">
            <template slot-scope="scope">
              <i class="el-icon-time"></i>
              <span style="margin-left: 10px">{{ scope.row.date }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="type"
            label="收支类型"
            align='center'
            width="150">
          </el-table-column>
          <el-table-column
            prop="describe"
            label="收支描述"
            align='center'
            width="180">
          </el-table-column>
          <el-table-column
            prop="income"
            label="收入"
            align='center'
            sortable
            width="170">
            <template slot-scope="scope">  
                <span style="color:#00d053">+ {{ scope.row.income }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="expend"
            label="支出"
            align='center'
            sortable
            width="170">
            <template slot-scope="scope">  
                <span style="color:#f56767">- {{ scope.row.expend }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="cash"
            label="账户现金"
            align='center'
            sortable
            width="170">
            <template slot-scope="scope">  
                <span style="color:#4db3ff"> {{ scope.row.cash }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            align='center'
            width="220">
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150" align='center' v-if="user.identity == 'manager'">
            <template slot-scope="scope">
              <el-button
                size="small"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="noData" v-if="!tableData.length"></div>
      </div>
      <!-- 分页 -->
      <el-row>
        <el-col :span="24">
          <div class="pagination">
            <Pagination :sizeOption="sizeOption" :pageSize="pageSize" :total="total" @pageChange="pageChange"/>
          </div>
        </el-col>
      </el-row>
      <Dialog :dialog="dialog" @update="getProfiles()" :form="form"/>
    </div>
</template>

<script>
import  { mapGetters } from 'vuex'
import Dialog from '../components/Dialog'
import Pagination from '../components/Pagination'
export default {
  name: 'FoundList',
  data () {
    return {
      sizeOption: [5, 10, 15, 20, 25],
      search_data: {
        startTime: '',
        endTime: ''
      },
      total: 1,
      pageSize: 5,
      pageNo: 1,
      tableData: [],
      allTableData: [],
      filterTimeData: [],
      search_data: {
        startTime: '',
        endTime: ''
      },
      dialog: {
        show: false,
        option: ''
      },
      form: {
        type: '',
        describe: '',
        income: '',
        expend: '',
        cash: '',
        remark: '',
        id: ''
      }
    }
  },
  components: {
    Dialog,
    Pagination
  },
  computed: {
    ...mapGetters(['user'])
  },
  methods: {
    getProfiles() {
      this.$axios.get('/api/profiles')
        .then(res=>{
          this.allTableData = res.data
          this.filterTimeData = res.data
          this.total = this.allTableData.length
          //页面初始化，当前页为第一页， 每页5条
          this.pageSize = this.sizeOption[0] || 5;
          this.pageNo = 1;
          this.setPageOption()
        })
        .catch(err=>{
          console.log(err)
        })
    },
    handleEdit(index, row) {
      this.dialog = {
        show: true,
        option: 'edit',
        title: '修改资金信息'
      }
      this.dialog.option = 'edit'
      this.form = {
        type: row.type,
        describe: row.describe,
        income: row.income,
        expend: row.expend,
        cash: row.cash,
        remark: row.remark,
        id: row._id
      }
    },
    handleDelete(index, row) {
      this.$axios.delete(`/api/profiles/delete/${row._id}`).then((res)=>{
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getProfiles()
      }).catch((err)=>{
        console.log(err)
      })
    },
    handelAdd() {
      this.form = {
        type: '',
        describe: '',
        income: '',
        expend: '',
        cash: '',
        remark: '',
        id: ''
      }      
      this.dialog.option = 'add'
      this.dialog.show = true
    },
    setPageOption() {
      this.total = this.allTableData.length
      this.tableData = this.allTableData.filter((item, index)=>{
        return index < this.pageSize;
      })
    },
    setPageChangeData() {
      //获取当前页码数
      let sortnum = (this.pageNo - 1) * this.pageSize

      //
      let table = this.allTableData.filter((item, index)=>{
        return index >= sortnum
      })

      this.tableData = table.filter((item, index) => {
        return index < this.pageSize
      })
    },
    pageChange(objVal) {
      this.pageSize = objVal.pageSize
      this.pageNo = objVal.pageNo
      if (this.pageNo == 1) {
        this.setPageOption()
      } else {
        this.setPageChangeData()
      }
    },
    onScreeoutMoney() {
      if (!this.search_data.startTime || !this.search_data.endTime) {
        this.$message({
          message: '请选择时间区间',
          type: 'warning'
        })
        this.getProfiles();
        return
      }

      const startTime = this.search_data.startTime.getTime();
      const endTime = this.search_data.endTime.getTime();

      this.allTableData = this.filterTimeData.filter((item)=>{
        let date = new Date(item.date);
        let time = date.getTime();
        return time <= endTime && time >= startTime;
      })
      this.setPageOption()

    }
  },
  created () {
    this.getProfiles();
  }
}
</script>


<style lang="less" scoped>

.noData{
    line-height: 26px;
    padding: 230px 0;
    margin-right: 0;
    height: 26px;
    background: url(~@/assets/empty-data.png) 50% no-repeat;
    text-align: center;
}
.foundlist {
  width: 100%;
  height: 100%;
  padding: 16px;
  box-sizing: border-box;
}
.search_add{
  margin-bottom: 10px;
  overflow: hidden; 
}
.rightBtn{
  float: right;
}
.pagination{
  text-align: right;
  margin-top: 10px;
}
</style>

